// Vuex的UI权限模块
const uiAuthModule = {
  namespaced: true,
  actions: {
    refreshUiAuths(context, value) {
      context.commit('reComputeUiAuths', value)
    },
  },
  mutations: {
    // 重新计算UI的可用性
    reComputeUiAuths(state, value) {
      if (state.uiAuthsStr != null) {
        const newUiAuths = JSON.parse(state.uiAuthsStr)
        // 如果需要鉴权
        if (newUiAuths.authValidateIsNeeded) {
          // 导航显示设置
          newUiAuths.navigationList.forEach(navGroup => {
            navGroup.finalStatus = value || navGroup.finalStatus
            navGroup.children.forEach(navItem => {
              navItem.finalStatus = value || navItem.finalStatus
            })
          })

          // 具体操作显示设置
          for (const groupName in newUiAuths.operation) {
            // 取到每一项导航组
            const navigationGroup = newUiAuths.operation[groupName]
            for (const itemName in navigationGroup) {
              // 每一项导航组下的导航项
              const navigationItem = navigationGroup[itemName]
              for (const operationName in navigationItem) {
                // 每一个导航项下的每一项具体操作可用性设置
                const operation = navigationItem[operationName]
                operation.finalStatus = value || operation.finalStatus
              }
            }
          }
        }
        // 使用新计算的UI权限
        state.uiAuths = newUiAuths
      }
    },
  },
  state: {
    // ui权限,含三个属性(navigationList、operation、authValidateIsNeeded)
    uiAuths: null,
    // ui权限字符串（用于保存原始权限信息）
    uiAuthsStr: null,
  },
  getters: {
    //  动态导航级联:导航组——>导航项
    dynamicNavigationList(state) {
      if (state.uiAuths != null)
        return state.uiAuths.navigationList
      return null
    },
    // #region 权限集合开始
    // UI权限总开关
    uiAuthValidateIsNotNeeded(state) {
      if (state.uiAuths != null)
        return !state.uiAuths.authValidateIsNeeded
      return true
    },
    // #region 【system】模块权限开始
    // 用户管理权限集合
    userAuths(state) {
      const { uiAuths: { operation: { system: { user } } } } = state
      return user
    },
    // 角色管理权限集合
    roleAuths(state) {
      const { uiAuths: { operation: { system: { role } } } } = state
      return role
    },
    // 接口管理权限集合
    apiAuths(state) {
      const { uiAuths: { operation: { system: { api } } } } = state
      return api
    },
    // 导航管理权限集合
    navgationAuths(state) {
      const { uiAuths: { operation: { system: { navigation } } } } = state
      return navigation
    },
    // 字体图标管理权限集合
    iconfontAuths(state) {
      const { uiAuths: { operation: { system: { iconfont } } } } = state
      return iconfont
    },
    // #endregion system模块权限结束


    // #region 【file】模块权限开始
    // 上传音频的权限集合
    audioAuths(state) {
      const { uiAuths: { operation: { file: { upload_audio } } } } = state
      return upload_audio
    },
    // 上传音频的权限集合
    videoAuths(state) {
      const { uiAuths: { operation: { file: { upload_vedio } } } } = state
      return upload_vedio
    },
    // 上传图片的权限集合
    imageAuths(state) {
      const { uiAuths: { operation: { file: { upload_image } } } } = state
      return upload_image
    },
    // 服务端物理文件管理权限集合
    servePhysicFileAuths(state) {
      const { uiAuths: { operation: { file: { server_physic_file } } } } = state
      return server_physic_file
    },
    // #endregion file模块权限结束


    //#region 【富文本】模块权限开始
    // quill_editor博客权限集合
    quillEditorAuths(state) {
      const { uiAuths: { operation: { richText: { quillEditor } } } } = state
      return quillEditor
    }
    //#endregion 【富文本】模块权限结束
    // #endregion 权限集合结束
  },
}
export default uiAuthModule